package cn.itcast.web.task;

import cn.itcast.domain.cargo.Contract;
import cn.itcast.service.cargo.ContractService;
import cn.itcast.web.controller.BaseController;
import cn.itcast.web.controller.cargo.ContractController;
import com.alibaba.dubbo.config.annotation.Reference;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 定时执行的任务类
 */
@Component
public class MyTask extends BaseController {

  ///**
  // * 定时执行的任务方法
  // */
  //public void execute(){
  //    // 业务处理
  //    // 测试
  //    System.out.println("每5秒执行一次方法：" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
  //}
  //
  //// 注入rabbitTemplate，发送消息
  // 注入rabbitTemplate，发送消息
  @Autowired
  private RabbitTemplate rabbitTemplate;

  @Reference
  private ContractService contractService;

  //  查询购销合同
  public void findByCompanyId() {

    List<Contract> contractList = contractService.findByThreeDay();

    //  遍历合同集合
    if (contractList != null && contractList.size() > 0) {
      for (Contract contract : contractList) {
        String email = contractService.findByCreateId(contract.getCreateBy());
        //System.out.println(email);
        sendMsg(email);
      }
    }

  }

  private void sendMsg(String email) {

    if (email != null && !"".equals(email)) {
      // 处理发送邮件的业务
      String subject = "货物到期通知";
      String content = "您的货物发货期限已经到期哦";
      // 发送消息
      Map<String, String> map = new HashMap<>();
      map.put("to", email);
      map.put("subject", subject);
      map.put("content", content);
      rabbitTemplate.convertAndSend("msg.email", map);
    }


  }


}
